/*
 * ServiceRepairGUI.java
 *
 * Created on March 28, 2007, 4:09 PM
 */

package edu.byu.isys413.jjenkins.view;

import edu.byu.isys413.jjenkins.data.*;
import java.sql.Date;
import java.util.List;
import javax.swing.JOptionPane;

/**
 * This form is create to initiate the service repair process
 * @author byu
 */
public class ServiceRepairGUI extends javax.swing.JFrame {
    
    ServiceRepairBO service;
    List<EmployeeBO> empList;
    
    // used to tell if there is a product associated with this order
    static boolean product;
    
    private static ServiceRepairGUI instance = null;
    
    
    /**
     * gets a single of this class
     */
    public static synchronized ServiceRepairGUI getInstance() {
        
        if (instance == null) {
            instance = new ServiceRepairGUI();
        }
        product = false;
        return instance;
    }
    
    /** Creates new form ServiceRepairGUI */
    public ServiceRepairGUI() {
        initComponents();
        this.populateList();
                this.setLocationRelativeTo(null);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel4 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtStartDate = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtEndDate = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtLabor = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        btnFindProduct = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        cmbEmployee = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        txtPrice = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDescription = new javax.swing.JTextArea();
        jPanel3 = new javax.swing.JPanel();
        btnSave = new javax.swing.JButton();

        getContentPane().setLayout(new java.awt.FlowLayout());

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Service and Repair Order Form");
        jPanel4.setPreferredSize(new java.awt.Dimension(300, 400));
        jLabel5.setText("Service and Repair");
        jLabel5.setBorder(javax.swing.BorderFactory.createMatteBorder(0, 0, 1, 0, new java.awt.Color(0, 0, 0)));
        jLabel5.setPreferredSize(new java.awt.Dimension(250, 14));
        jPanel4.add(jLabel5);

        jPanel1.setLayout(new java.awt.GridLayout(6, 2, 8, 8));

        jPanel1.setPreferredSize(new java.awt.Dimension(250, 190));
        jLabel1.setText("Date Started:");
        jPanel1.add(jLabel1);

        txtStartDate.setText("yyyy-mm-dd");
        jPanel1.add(txtStartDate);

        jLabel2.setText("Date Completed:");
        jPanel1.add(jLabel2);

        txtEndDate.setText("yyyy-mm-dd");
        jPanel1.add(txtEndDate);

        jLabel3.setText("Labor Hours:");
        jPanel1.add(jLabel3);

        jPanel1.add(txtLabor);

        jLabel6.setText("Product:");
        jPanel1.add(jLabel6);

        btnFindProduct.setText("Find Product...");
        btnFindProduct.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFindProductActionPerformed(evt);
            }
        });

        jPanel1.add(btnFindProduct);

        jLabel4.setText("Employee:");
        jPanel1.add(jLabel4);

        cmbEmployee.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-" }));
        jPanel1.add(cmbEmployee);

        jLabel8.setText("Price:");
        jPanel1.add(jLabel8);

        jPanel1.add(txtPrice);

        jPanel4.add(jPanel1);

        jPanel2.setPreferredSize(new java.awt.Dimension(250, 120));
        jPanel2.setRequestFocusEnabled(false);
        jLabel7.setText("Description:");
        jPanel2.add(jLabel7);

        txtDescription.setColumns(20);
        txtDescription.setRows(5);
        txtDescription.setPreferredSize(new java.awt.Dimension(130, 94));
        jScrollPane1.setViewportView(txtDescription);

        jPanel2.add(jScrollPane1);

        jPanel4.add(jPanel2);

        jPanel3.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jPanel3.setPreferredSize(new java.awt.Dimension(250, 100));
        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        jPanel3.add(btnSave);

        jPanel4.add(jPanel3);

        getContentPane().add(jPanel4);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        
        // Gets the product under warrenty
        if(product == true){
            try{
                service.setTransaction(PickTransLineGUI.getInstance().selectedLine);
            }catch(Exception ex){
                // I choose not to do anything here. If a item does not exist the program will sipmle continue on
                System.out.println("No Product for repair");
            }
        }
        
        
        // Gets the date started
        try{
            if(!(txtStartDate.getText().equals(""))){
                System.out.println("date is" +txtStartDate.getText());
                String date = txtStartDate.getText();
                String[] components = date.split("-");
                int month = Integer.parseInt(components[1]);
                int day = Integer.parseInt(components[2]);
                int year = Integer.parseInt(components[0]) - 1900;
                
                //This has been depreciated. Consider using another alternative. Possibly storing date as string
                service.setDateStarted(new Date(year, month -1, day));
            }
            
        }catch(Exception ex){
            // I don't do anything here. This just mean that they did not enter a date
            System.out.println("no date");
        }
        
        // Gets the date completed
        try{
            if(!(txtEndDate.getText().equals(""))){
                String date = txtEndDate.getText();
                String[] components = date.split("-");
                int month = Integer.parseInt(components[1]);
                int day = Integer.parseInt(components[2]);
                int year = Integer.parseInt(components[0]) - 1900;
                
                //This has been depreciated. Consider using another alternative. Possibly storing date as string
                service.setDateCompleted(new Date(year, month -1, day));
            }
            
        }catch(Exception ex){
            // I don't do anything here. This just mean that they did not enter a date
            System.out.println("no date");
        }
        
        // Sets the description
        service.setDescription(txtDescription.getText());
        
        //Sets teh labor hours
        if(!(txtLabor.getText().equals(""))){
            System.out.println(txtLabor.getText());
            service.setLaborHours(Double.parseDouble(txtLabor.getText()));
        }
        
        // Sets the employee
        if(cmbEmployee.getSelectedIndex()!= 0){
            service.setEmployee(empList.get(cmbEmployee.getSelectedIndex()-1));
        }
        
        if(!(txtPrice.getText().equals(""))){
            service.setPrice(Double.parseDouble(txtPrice.getText()));
        }
         try {
            
            RevenueSourceFactory.getInstance().getDAO("service_repair").save(service);
        } catch (DataException ex) {
             JOptionPane.showMessageDialog(null, ex.toString(),"Cannot Save Service and Repair", JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
        }
        
        NewOrder.getInstance().populateTransactionLines();
        this.dispose();
    }//GEN-LAST:event_btnSaveActionPerformed
    
    
    private void btnFindProductActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFindProductActionPerformed
        try{
            GetTransaction.getInstance().getLine(service.getLine().getTransaction().getCustomer());
            product = true;
        }catch(DataException ex){
            JOptionPane.showMessageDialog(null,ex.toString(),"Cannot get product", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnFindProductActionPerformed
    
    /**
     * populates the respective information
     * @param service 
     */
    public void populate(ServiceRepairBO service){
        
        this.service = service;
        this.setVisible(true);
        
    }
    
    /**
     * populates the list to be displayed
     */
    public void populateList(){
        
        try{
            empList = EmployeeDAO.getInstance().readAll();
            
            for(int i = 0; i<empList.size();i++){
                EmployeeBO tempEmp = empList.get(i);
                cmbEmployee.addItem(tempEmp.getName());
            }
        }catch(Exception ex){
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null,ex.toString(),"Cannot populate emplyees", JOptionPane.ERROR_MESSAGE);
            
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ServiceRepairGUI().setVisible(true);
            }
        });
    }
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnFindProduct;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cmbEmployee;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea txtDescription;
    private javax.swing.JTextField txtEndDate;
    private javax.swing.JTextField txtLabor;
    private javax.swing.JTextField txtPrice;
    private javax.swing.JTextField txtStartDate;
    // End of variables declaration//GEN-END:variables
    
}
